package server;

import index.planner.IndexUpdatePlanner;
import storage.buffer.BufferMgr;
import storage.file.FileMgr;
import storage.log.LogMgr;
import storage.metadata.MetadataMgr;
import storage.tx.Transaction;
import engine.planner.BasicQueryPlanner;
import engine.planner.Planner;
import engine.planner.QueryPlanner;
import engine.planner.UpdatePlanner;

public class starDB {
   public static int BUFFER_SIZE = 8;
   public static String LOG_FILE = "stardb.log";
   
   private static FileMgr     fm;
   private static BufferMgr   bm;
   private static MetadataMgr mm;
   private static LogMgr logm;

   
 
  
   public static void init(String dirname) {
      fm = new FileMgr(dirname);
      logm = new LogMgr(LOG_FILE);
      bm = new BufferMgr(BUFFER_SIZE);
      Transaction tx = new Transaction();
      boolean isNew = fm.isNew();
      mm = new MetadataMgr(isNew, tx);
      if(isNew) {
    	  System.out.println("Create a new database.");
      } else {
    	  System.out.println("Recover existing database.");
    	  tx.recover();
      }
      tx.commit();
   }
   
   public static LogMgr logMgr() {     //here is not complete
	   return logm;
   }
   
   public static FileMgr fileMgr() { 
	   return fm; 
   }
   
   public static BufferMgr bufferMgr() { 
	   return bm; 
   }
   
   public static MetadataMgr mdMgr() { 
	   return mm; 
   }

   public static Planner planner() {
	   QueryPlanner  qplanner = new BasicQueryPlanner();
	   UpdatePlanner uplanner = new IndexUpdatePlanner();
	   return new Planner(qplanner, uplanner);
   }
}
